<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Function Multiversioning (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Function Multiversioning (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Function Multiversioning (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002b_002b-Extensions.html" rel="up" title="C++ Extensions">
<link href="Type-Traits.html" rel="next" title="Type Traits">
<link href="C_002b_002b-Attributes.html" rel="prev" title="C++ Attributes">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en_US">
<div class="section-level-extent" id="Function-Multiversioning">
<div class="nav-panel">
<p>
Next: <a href="Type-Traits.html" accesskey="n" rel="next">Type Traits</a>, Previous: <a href="C_002b_002b-Attributes.html" accesskey="p" rel="prev">C++-Specific Variable, Function, and Type Attributes</a>, Up: <a href="C_002b_002b-Extensions.html" accesskey="u" rel="up">Extensions to the C++ Language</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Function-Multiversioning-1"><span>7.8 Function Multiversioning<a class="copiable-link" href="#Function-Multiversioning-1"> &para;</a></span></h3>
<a class="index-entry-id" id="index-function-versions"></a>

<p>With the GNU C++ front end, for x86 targets, you may specify multiple
versions of a function, where each function is specialized for a
specific target feature.  At runtime, the appropriate version of the
function is automatically executed depending on the characteristics of
the execution platform.  Here is an example.
</p>
<div class="example smallexample">
<pre class="example-preformatted">__attribute__ ((target (&quot;default&quot;)))
int foo ()
{
  // The default version of foo.
  return 0;
}

__attribute__ ((target (&quot;sse4.2&quot;)))
int foo ()
{
  // foo version for SSE4.2
  return 1;
}

__attribute__ ((target (&quot;arch=atom&quot;)))
int foo ()
{
  // foo version for the Intel ATOM processor
  return 2;
}

__attribute__ ((target (&quot;arch=amdfam10&quot;)))
int foo ()
{
  // foo version for the AMD Family 0x10 processors.
  return 3;
}

int main ()
{
  int (*p)() = &amp;foo;
  assert ((*p) () == foo ());
  return 0;
}
</pre></div>

<p>In the above example, four versions of function foo are created. The
first version of foo with the target attribute &quot;default&quot; is the default
version.  This version gets executed when no other target specific
version qualifies for execution on a particular platform. A new version
of foo is created by using the same function signature but with a
different target string.  Function foo is called or a pointer to it is
taken just like a regular function.  GCC takes care of doing the
dispatching to call the right version at runtime.  Refer to the
<a class="uref" href="https://gcc.gnu.org/wiki/FunctionMultiVersioning">GCC wiki on
Function Multiversioning</a> for more details.
</p>
</div>



</body>
</html>
